import 'dart:convert';

import 'package:flutter/material.dart';
import 'package:flutter_spinkit/flutter_spinkit.dart';
import '../../helper/Helper.dart';

import '../../model/inspectreport.dart';
import '../../pages/gmap/gpsHelper.dart';

import '../../widgets/IconWidget.dart';
import '../../widgets/inputWidget.dart';

import 'package:wakelock/wakelock.dart';

class InspectAddEndPage extends StatefulWidget {
  @override
  InspectAddEndPage();
  @override
  InspectAddEndPageState createState() => new InspectAddEndPageState();
}

class InspectAddEndPageState extends State<InspectAddEndPage> {
  bool ifLoaded = false;
  late InspectReport x1;

  final TextEditingController c1 = new TextEditingController();
  final TextEditingController c2 = new TextEditingController();
  final TextEditingController c3 = new TextEditingController();
  final TextEditingController c4 = new TextEditingController();
  final TextEditingController c5 = new TextEditingController();
  final TextEditingController c6 = new TextEditingController();
  final TextEditingController c7 = new TextEditingController();
  final TextEditingController c8 = new TextEditingController();

  @override
  void initState() {
    // TODO: implement initState
    super.initState();
    loadData();
  }

  loadData() async {
    var d1 = await Helper.localDB.getString("InspectData");
    x1 = InspectReport.fromJson(jsonDecode(d1!));
    c1.text = x1.Wt1;
    c2.text = x1.Wt2;
    c3.text = x1.Wt3;
    c4.text = x1.Wt4;
    c5.text = x1.Wt5;
    c6.text = x1.Wt6;
    c7.text = x1.Wt7;

    setState(() {
      ifLoaded = true;
    });
  }

  saveData(BuildContext context) async {
    x1.Wt1 = c1.text;
    x1.Wt2 = c2.text;
    x1.Wt3 = c3.text;
    x1.Wt4 = c4.text;
    x1.Wt5 = c5.text;
    x1.Wt6 = c6.text;
    x1.Wt7 = c7.text;

    await Helper.localDB.setString("InspectData", jsonEncode(x1));
  }

  upload(BuildContext context) async {
    x1.Wt1 = c1.text;
    x1.Wt2 = c2.text;
    x1.Wt3 = c3.text;
    x1.Wt4 = c4.text;
    x1.Wt5 = c5.text;
    x1.Wt6 = c6.text;
    x1.Wt7 = c7.text;
    x1.EndTM = Helper.DateTimeNowString();
    x1.MinutesLast = Helper.DateTimeByNow(x1.CreateTM).toDouble();
    var gpsList = GpsHelper.StringToPointList(x1.GpsRemark);
    x1.Distance = GpsHelper.GetDistance(gpsList);
    x1.GpsRemark = GpsHelper.PointListToString(gpsList);

    //print(x1.toJson());
    var pb1 = await InspectReport.Add(x1);

    Wakelock.disable();
    if (pb1 == true) {
      await Helper.localDB.remove("InspectData");
      Navigator.of(context).pop();
    }
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      //backgroundColor: Colors.white,
      body: InspectAddEndBody(context),
    );
  }

  List<Widget> getWidgetList(BuildContext context) {
    List<Widget> list1 = <Widget>[];

    List<Widget> list2 = <Widget>[];
    List<Widget> list3 = <Widget>[];

    list1.add(InputWidget.inCard(
      ListTile(
        leading: IconWidget.InspectReportImage(),
        title: Text(
          x1.CreateTM,
        ),
      ),
    ));
    list2.add(InputWidget.getInput("船只", c1));
    list2.add(Divider(
      height: 0.0,
    ));
    list2.add(InputWidget.getInput("码头", c2));
    list2.add(Divider(
      height: 0.0,
    ));
    list2.add(InputWidget.getInput("非法行为", c3));
    list2.add(Divider(
      height: 0.0,
    ));
    list2.add(InputWidget.getInput("库岸边坡", c4));
    list2.add(Divider(
      height: 0.0,
    ));
    list2.add(InputWidget.getInput("库区漂浮物", c5));
    list2.add(Divider(
      height: 0.0,
    ));
    list2.add(InputWidget.getInput("库区水质", c6));
    list2.add(Divider(
      height: 0.0,
    ));
    list2.add(InputWidget.getInput2("其他异常", c7));
    list2.add(Divider(
      height: 0.0,
    ));
    list1.add(InputWidget.inCard2(list2));

    list1.add(InputWidget.inCard(
        InputWidget.getButton("保存报告", () => saveData(context))));
    list1.add(InputWidget.inCard(
        InputWidget.getButton("结束巡查", () => upload(context))));
    //list1.add(getCLWidget(s1.State==6, Helper.DateTimeFormat(s1.HZQMTM), s1.HZQM));
    return list1;
  }

  InspectAddEndBody(BuildContext context) => SingleChildScrollView(
        child: InspectAddEndFields(context),
      );

  InspectAddEndFields(BuildContext context) {
    if (!ifLoaded)
      return Center(
          child: SpinKitCircle(
        color: Colors.blue,
      ));
    return Container(child: Column(children: getWidgetList(context)));
  }
}
